package com.yxhl.zoume.core.tripsmgmt.ui.fragment.home;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.zxing.WriterException;
import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.fragment.BaseFragment;
import com.yxhl.zoume.core.tripsmgmt.ui.activity.TicketCodeActivity;
import com.yxhl.zoume.utils.EncodeHelper;

import butterknife.BindView;

/**
 * Created by vhawk on 15/9/30.
 * 车票展示页面
 */
public class TicketCodeFragment extends BaseFragment {

    public static final String TICKET_KEY = "ticket_code_key";
    private static final String TAG = "TicketCodeFragment";
    @BindView(R.id.tv_generate_ticket)
    TextView tv_generate_ticket;
    @BindView(R.id.iv_picture)
    ImageView iv_picture;
    @BindView(R.id.iv_picture_hor)
    ImageView iv_picture_hor;

    //arguments
    private String mVerifyCode;


    public static TicketCodeFragment newInstance(String verifyCode) {
        TicketCodeFragment ticketCodeFragment = new TicketCodeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TICKET_KEY, verifyCode);
        ticketCodeFragment.setArguments(bundle);
        return ticketCodeFragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleArguments();
    }

    private void handleArguments() {
        Bundle arguments = getArguments();
        if (arguments != null) {
            mVerifyCode = arguments.getString(TICKET_KEY);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        if (!TextUtils.isEmpty(mVerifyCode)) {
            createTicket(mVerifyCode);
            initData(mVerifyCode);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
    }

    private void initData(String verifyCode) {
        if (!TextUtils.isEmpty(verifyCode)) {
            tv_generate_ticket.setText(verifyCode);
        }
    }

    private void createTicket(String contentString) {

        try {
            if (!TextUtils.isEmpty(contentString)) {

                EncodeHelper helper = new EncodeHelper();
                Bitmap qrCodeBitmap = helper.createQRCode(contentString, 350);
                iv_picture.setImageBitmap(qrCodeBitmap);
                Bitmap qrCodeBitmapHor = helper.createOneDCode(contentString);
                iv_picture_hor.setImageBitmap(qrCodeBitmapHor);
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void initializeInjector() {

    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_reservation_ticket;
    }

    @Override
    protected void setupToolBar() {
        ((TicketCodeActivity) mActivity).setToolbarTitle(getString(R.string.my_ticket));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
